package com.jash.designpack2;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, RadioGroup.OnCheckedChangeListener, ViewPager.OnPageChangeListener, View.OnTouchListener {

//    private DrawerLayout drawer;
    private RadioGroup group;
    private ViewPager pager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//        drawer = (DrawerLayout) findViewById(R.id.drawer);
//        setSupportActionBar(toolbar);
//        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, 0, 0);
//        drawer.addDrawerListener(toggle);
//        toggle.syncState();
        NavigationView navigationView = (NavigationView) findViewById(R.id.navigation);
        navigationView.setNavigationItemSelectedListener(this);
        pager = (ViewPager) findViewById(R.id.pager);
        pager.setOnTouchListener(this);
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            list.add(String.format(Locale.getDefault(), "第%02d页", i + 1));
        }
        MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager(), list);
        pager.setAdapter(adapter);
        TabLayout tab = (TabLayout) findViewById(R.id.tab);
        tab.setupWithViewPager(pager);
        group = (RadioGroup) findViewById(R.id.group);
        group.setOnCheckedChangeListener(this);
        pager.addOnPageChangeListener(this);
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.item_1:
                break;
            case R.id.item_2:
                break;
            case R.id.item_3:
                break;
            case R.id.item_4:
                break;
            case R.id.item_5:
                Intent intent = new Intent(this, AlbumActivity.class);
                startActivity(intent);
                break;
        }
//        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i) {
            case R.id.btn_1:
                pager.setCurrentItem(0, false);
                break;
            case R.id.btn_2:
                pager.setCurrentItem(1, false);
                break;
            case R.id.btn_3:
                pager.setCurrentItem(2, false);
                break;
            case R.id.btn_4:
                pager.setCurrentItem(3, false);
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        RadioButton btn_1 = (RadioButton) group.getChildAt(position);
        int startColor = 0xff888888;
        int endColor = 0xff00ff00;
        for (int j = 0; j < group.getChildCount(); j++) {
            RadioButton button = (RadioButton) group.getChildAt(j);
            Drawable top = button.getCompoundDrawables()[1];
            if (j == position) {
                button.setTextColor(0xff00ff00);
                DrawableCompat.setTint(top, 0xff00ff00);
            } else {
                button.setTextColor(0xff888888);
                DrawableCompat.setTint(top, 0xff888888);
            }
        }
        int rgb = Color.rgb((int) (0x88 * positionOffset), (int) (0xff - 0x88 * positionOffset), (int) (0x88 * positionOffset));
        btn_1.setTextColor(rgb);
        Drawable drawable = btn_1.getCompoundDrawables()[1];
        DrawableCompat.setTint(drawable, rgb);
        if (position + 1 < group.getChildCount()) {
            RadioButton btn_2 = (RadioButton) group.getChildAt(position + 1);
            positionOffset = 1 - positionOffset;
            int rgb_2 = Color.rgb((int) (0x88 * positionOffset), (int) (0xff - 0x88 * positionOffset), (int) (0x88 * positionOffset));
            btn_2.setTextColor(rgb_2);
            Drawable drawable1 = btn_2.getCompoundDrawables()[1];
            DrawableCompat.setTint(drawable1, rgb_2);
        }
    }

    @Override
    public void onPageSelected(int position) {
        group.check(group.getChildAt(position).getId());

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        switch (motionEvent.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (motionEvent.getX() > view.getWidth() / 5) {
                    pager.requestDisallowInterceptTouchEvent(true);
                }
                break;
        }
        return false;
    }
}
